package com.cn.lxz.android.aws;

import com.amazonaws.AmazonClientException;
import com.amazonaws.event.ProgressEvent;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.CompleteMultipartUploadResult;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.ProgressListener;
import com.amazonaws.services.s3.model.UploadPartRequest;
import com.amazonaws.services.s3.model.UploadPartResult;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LowLevel {
    public String bucketName;
    public String objectKey;
    private AmazonS3Client client;
    /**初始化分片上传*/
    public void init(){
        //指定容器与对象名，也可以在request中同时设置metadata，acl等对象的属性
        InitiateMultipartUploadRequest initUploadRequest = new
                InitiateMultipartUploadRequest(bucketName, objectKey);
        InitiateMultipartUploadResult initResult =
                client.initiateMultipartUpload(initUploadRequest);
         //用于区分分片上传的唯一标识，后续的操作中会使用该id
        String uploadId = initResult.getUploadId();
    }
    /**上传分片*/
    public void uploadId(String uploadId,InputStream instream,long size,int partId){
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();

        // 添加进度监听并打印进度
        uploadPartRequest.setProgressListener(new ProgressListener() {
            @Override
            public void progressChanged(com.amazonaws.services.s3.model.ProgressEvent progressEvent) {
                long bytesTransferred = progressEvent.getBytesTransferred();
                System.out.println("分片 " + partId + " 上传进度：已传输 " + bytesTransferred + " 字节，共 " + size + " 字节");
            }
        });

        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(objectKey);
        uploadPartRequest.setUploadId(uploadId);
        //设置输入流
        uploadPartRequest.setInputStream(instream);
        //设置分片大小，最小为4MB
        uploadPartRequest.setPartSize(size);
        //设置分片号
        uploadPartRequest.setPartNumber(partId);
        UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
    }

    public void Complete(String uploadId,List<PartETag> partETags){
        //将partEtag按照分片号升序排序
        Collections.sort(partETags, new Comparator<PartETag>() {
            public int compare(PartETag p1, PartETag p2) {
                return p1.getPartNumber() - p2.getPartNumber();
            }
        });
        CompleteMultipartUploadRequest completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId,
                        partETags);
        //成功完成分片上传后，服务端会返回该对象的信息
        CompleteMultipartUploadResult result =
                client.completeMultipartUpload(completeMultipartUploadRequest);
    }
}
